package Java常用类的学习;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class HashSetTeset {
    public static void main(String[] args) {
        Collection c=new HashSet();
        c.add(new Person("李晓辉",25));
        c.add(new Person("王欣",26));
        System.out.println(c.size());
        System.out.println(c);
        c.add(new Integer(5));
        c.add(5);
        c.add("hello world");
        System.out.println(c);
        c.remove(new Integer(5));
        c.remove("hello world");
        System.out.println(c.remove(new Person("王欣",26)));
        System.out.println(c);
        System.out.println("____________________TreeSet_______________");


        //TreeSet 实现了SortedSort接口，能对容器中的对象进行排序，当向TreeSet中加入一个对象后，会继续保持对象间的次序
        Set set=new TreeSet();
        set.add(new String("spring"));
        set.add(new String("summer"));
        set.add(new String("autumn"));
        set.add(new String("winter"));
        System.out.println(set);


    }
}
class Person{
    private String name;
    private int age;
    public Person(String n,int a){
            this.name=n;
            this.age=a;
    }
    public boolean equals(Object obj){
        if(obj instanceof Person){
            Person person=(Person)obj;
            return (name.equals(person.name)&&(age==person.age));
        }

        return super.equals(obj);
    }
    //如果把这个hashCode方法取消后会运行结果不一样
    /*public int hashCode(){
        int i;
        i=(name==null?0:name.hashCode());
        i=10*i+age;
        return i;
    }*/
}
